<?php

namespace app\admin\controller;

use app\models\Admin;
use app\models\{
    User as Users,
    Moneylog,
    Shopinfo,
    Project,
    MerchantItem,
    Config
};
use app\facade\Redis;
use think\Db;

class Merchant extends Base
{

    public function merchantitem()
    {
        $data = input('param.');
        $model = new MerchantItem();
        $list = $model->order('id desc');
        if (isset($data['status'])) {
            $list = $list->where('status', $data['status']);
        }
        if (isset($data['shoptitle'])) {
            $shopid = (new Shopinfo())->where('title', $data['title'])->value('id') ?? 0;
            $list = $list->where('shopid', $shopid);
        }
        if (isset($data['title'])) {
            $list = $list->where('title', 'like', '%' . $data['title'] . '%');
        }
        $list = $list->paginate(20, false, ['query' => $this->request->param()]);
        foreach ($list as $lists) {
            $lists['user'] = (new Users())->where('id', $lists['uid'])->value('mobile');
            $lists['images'] = json_decode($lists['images'], true);
            $lists['shoptitle'] = (new Shopinfo())->where('id', $lists['shopid'])->value('title');
            $lists['type_text'] = (new Project())->where('id', $lists['project'])->value('name');
        }
        return view('', ['list' => $list]);
    }


    public function merchantitem_edit()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $data = input('post.');
            if (!empty($data['images'])) {
                $data['images'] = json_encode($data['images'], true);
            }
            //查询店铺是否存在
            $shopinfo = (new Shopinfo())->where('title', $data['shoptitle'])->find();
            if (empty($shopinfo)) {
                return json(['status' => 1, 'msg' => '店铺不存在']);
            }
            $data['uid'] = $shopinfo['userid'];
            $data['shopid'] = $shopinfo['id'];
            if (empty($data['id'])) {
                $result = (new MerchantItem())->allowField(true)->save($data);
            } else {
                $result = (new MerchantItem())->allowField(true)->save($data, ['id' => $data['id']]);
            }
            if ($result) {
                return json(['status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['status' => 1, 'msg' => '操作失败']);
            }
        } else {
            $id = $this->request->get('id');
            $row = (new MerchantItem())->where('id', $id)->find();
            if (!empty($row['images'])) {
                $row['shoptitle'] = (new Shopinfo())->where('id', $row['shopid'])->value('title');
                $row['images'] = json_decode($row['images'], true);
            }
            $project = (new Project())->select();
            return view('', ['row' => $row, 'project' => $project]);
        }
    }


    public function index()
    {
        $data = input('param.');
        $model = new Shopinfo();
        $list = $model->order('id desc');
        if (isset($data['status'])) {
            $list = $list->where('status', $data['status']);
        }
        if (isset($data['title'])) {
            $list = $list->where('title', 'like', '%' . $data['title'] . '%');
        }
        $list = $list->paginate(20, false, ['query' => $this->request->param()]);
        foreach ($list as $lists) {
            $lists['user'] = (new Users())->where('id', $lists['userid'])->value('mobile');
            $lists['images'] = json_decode($lists['images'], true);
            $lists['time'] = json_decode($lists['time'], true);
        }
        return view('', ['list' => $list]);
    }

    public function merchantstr()
    {
        $data = input('param.');
        if (empty($data['id']) || empty($data['status'])) {
            return json(['status' => 1, 'msg' => '参数错误!']);
        }
        $model = new Shopinfo();
        $shopinfo = $model->where('id', $data['id'])->find();
        if (empty($shopinfo)) {
            return json(['status' => 1, 'msg' => '商家信息不存在!']);
        }
        if ($shopinfo['status'] == 2) {
            return json(['status' => 1, 'msg' => '商家状态不可审核!']);
        }
        $res = $model->where('id', $data['id'])->update(['status' => $data['status']]);
        if ($res) {
            return json(['status' => 0, 'msg' => '操作成功!']);
        }
        return json(['status' => 1, 'msg' => '操作失败，请重试!']);
    }

    public function merchat_edit()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $data = input('post.');
            if (empty($data['latlon'])) {
                return json(['status' => 1, 'msg' => '店铺坐标不能为空']);
            } else {
                $latlon = explode(',', $data['latlon']);
                if (count($latlon) != 2) {
                    return json(['status' => 1, 'msg' => '店铺坐标错误']);
                }
                $data['lat'] = $latlon[0];
                $data['lon'] = $latlon[1];
            }
            if (!empty($data['images'])) {
                $data['images'] = json_encode($data['images'], true);
            }
            if (empty($data['id'])) {
                //查询用户是否存在和已有店铺
                $user = (new Users())->where('mobile', $data['phone'])->find();
                if (empty($user)) {
                    return json(['status' => 1, 'msg' => '用户不存在']);
                }
                $shopinfo = (new Shopinfo())->where('userid', $user['id'])->find();
                if (!empty($shopinfo)) {
                    return json(['status' => 1, 'msg' => '店铺已存在或待审核']);
                }
                $data['userid'] = $user['id'];
                $result = (new Shopinfo())->allowField(true)->save($data);
            } else {
                $result = (new Shopinfo())->allowField(true)->save($data, ['id' => $data['id']]);
            }
            if ($result) {
                return json(['status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['status' => 1, 'msg' => '操作失败']);
            }
        } else {
            $id = $this->request->get('id');
            $row = (new Shopinfo())->where('id', $id)->find();
            if (!empty($row['images'])) {
                $row['images'] = json_decode($row['images'], true);
            }
            return view('', ['row' => $row]);
        }
    }
}
